import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import * as adapter from '../../constants/adapter';
import { Toast, Bubble, Tools } from 'weaver-mobile';
import { Form } from 'weaver-mobile-page';
const getLabel = Tools.getLabel;
export default class ContacterFormStore {
    comObj = {};
    params = {};
    formData = []; // 更新前的原始数据
    @observable id = "";
    @observable customerId = "";
    @observable initResponsed = false; // 是否已经得到初始化数据的响应
    @observable initSuccessed = false; // 初始化数据是否获取成功
    @observable initFailedMsg = "";    // 初始化处理失败的原因
    @observable form = {};
    @observable saveLoading = false;
    @observable buttonProps = {loading:false,disabled:false};
    @observable isView=false;
    @observable isEdit=false;
    @observable operation="";
    @observable name="";
    @action
    doNew = (comObj, params) => {
        this.comObj = comObj;
        this.params = params;
        this.operation=params.operation;
        this.name=params.name;
        // console.log(comObj)
    }
    @action
    doInit = () => {
        let params = {...this.params,operation:this.operation};
        this.id = params.contacterId;
        this.customerId = params.customerId;
        Apis.getContacterForm(params).then(data => {
            this.doInitCallBack(params, data);
        });
    }


    @action
    doInitCallBack = (params, data) => {
        this.buttonProps = {loading:false,disabled:false};
        this.initResponsed = true;
        this.canEdit = data.canEdit;
        if (data.status == "success") {
            this.formData = data.datas;
            const currentPath = this.comObj.props.location.pathname;
            this.form = new Form.Store();
            this.form.initForm(data.datas, currentPath);
            this.isView=true;
            this.isEdit=false;
            this.initSuccessed = true;
        } else {
            this.initSuccessed = false;
            this.initFailedMsg = data.msgcode;
        }
    }
    //联系人卡片-编辑
    @action
    getEditForm=()=>{
        this.operation="edit";
        this.initResponsed=false;
        this.doInit();
    }
    //联系人卡片-编辑-取消编辑
    @action
    cancelEdit=()=>{
        this.operation="view";
        this.initResponsed=false;
        this.doInit();
    }
    @action
    doClear = () => {
        this.initResponsed = false;
        this.initSuccessed = false;
        this.initFailedMsg = "";
        this.operation="";
        this.form={};
        this.isEdit=false;
        this.isView=false;
    }

    @action
    doSave = () => {
        this.buttonProps={loading:true,disabled:true};
        const rpath = this.params.rpath;
        if (this.form.doValidate().pass) {
            if (this.operation == "add") {
                const params = { ...this.form.formParams };
                Apis.createContacter(params).then(data => {
                    if (data.status == "success") {
                        this.buttonProps={loading:false,disabled:false};
                        window.crmGlobal && window.crmGlobal.doSearch();
                        Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2, ()=>{
                            this.comObj.props.history.go(-1);
                            if(typeof(this.comObj.props.cardStore!=="undefined")&&this.comObj.props.cardStore){
                                this.comObj.props.cardStore.doInit();
                            }
                        });
                    } else {
                        Toast.fail(data.msgcode, 1);
                    }
                });
            } else {
                const newFormData = this.form.formParams;
                Apis.updateContacterBatch({contacterId:this.id,operation:"edit",...newFormData}).then(data => {
                    if(data.status == "failed"){
                        Bubble.error(data.msgcode, 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2);
                        return;
                    }
                    window.crmGlobal && window.crmGlobal.doSearch();
                    Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2, ()=>{
                        this.operation="view";
                        this.initResponsed = false;
                        this.doInit();
                    });

                });
            }
        }else{
            this.buttonProps = {loading:false,disabled:false};
        }
    }
    //删除联系人
    @action
    doDelete = () => {
        const params = { contacterId: this.id };
        Apis.contacterDelete(params).then(data => {
            if (data.status === "success") {
                Bubble.success(getLabel('83472', '删除成功!'), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, 
                () => {
                    this.comObj.props.history.go(-2);
                    if(typeof(this.comObj.props.cardStore)&&this.comObj.props.cardStore){

                        this.comObj.props.cardStore.doInit();
                    
                    }
                }
                );
            }else{
                Bubble.error(data.msgcode, 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2);
            }
        });
    }
}